import Vue from "vue";
import Vuex from "vuex";
import createPersistedState from "vuex-persistedstate";

Vue.use(Vuex);

const store = new Vuex.Store({
  plugins: [createPersistedState()],
  state: {
    token: "",
    username: "",
    password: "",
    uid: "",
    accountStatus: "", // 账号状态 0 正常 1 禁用
    editorState: "true", // 富文本编辑器默认激活状态
    code: "", // 响应状态码
    msg: "", // 状态描述
    breadList: [], // 面包屑导航
  },
  mutations: {
    Login(state, data) {
      state.token = data.token;
      state.username = data.name;
      state.password = data.pwd;
      state.uid = data.id;
      state.accountStatus = data.status;
      state.code = "0";
      state.breadList = data.breadList;
    },
    Logout(state) {
      state.token = "";
      state.username = "";
      state.password = "";
      state.uid = "";
      state.accountStatus = "";
      state.code = "";
      state.msg = "";
      state.breadList = [];
    },
    editAccountStatus(state, data) {
      state.accountStatus = data.status;
    },
    changeCode(state, data) {
      state.code = String(data.code);
      state.msg = data.msg;
    },
    editPwd(state, data) {
      state.password = data.newPwd;
    },
    // 激活富文本编辑器
    ActiveEditor(state) {
      state.editorState = "true";
    },
    // 禁用富文本编辑器
    DisableEditor(state) {
      state.editorState = "";
    },
    changeBreadcrumb(state, data) {
      state.breadList = [data.meta.title, data.name];
    },
  },
  actions: {
    changeBreadcrumb({ commit }, data) {
      setTimeout(() => {
        commit("changeBreadcrumb", data);
      }, 0);
    },
  },
  modules: {},
  getters: {},
});

export default store;
